package com.freestyle.multidatasource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 支持多线程环境下的data source type的存取
 * Created by rocklee on 2020/8/27 10:00
 */
public class DataSourceContextHolder {
  private static final ThreadLocal<String> contextHolder = new ThreadLocal();
  private static final List<String> dataSourceIds = Collections.synchronizedList( new ArrayList());

  public static void changeDataSource(String dataSource) {
    contextHolder.set(dataSource);
  }

  public static String getDataSource() {
    return (String) contextHolder.get();
  }
  public static void clearDataSource() {
    contextHolder.remove();
  }
  public static boolean containsDataSource(String dataSourceId) {
    return dataSourceIds.contains(dataSourceId);
  }

  public static List<String> getDataSourceIds() {
    return dataSourceIds;
  }

}
